<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Admin_Model_SystemUser extends ZF_DB{
    protected $_table_name = "system_user" ;
    protected $_id;
    protected $_username;
    protected $_password;
    protected $_role;
    protected $_email;
    protected $_active;
    
    public function getUsername(){
        return $this->_username;
    }
    public function setUsername($username){
        $this->_username = $username;
    }
    public function getId(){
        return $this->_id;
    }
    public  function setId($id){
        $this->_id = $id;
    }
    public function getPassword(){
        return $this->_password;
    }
    public function setPassword($password){
        $this->_password = sha1(md5($password.'system'));
    }
    public function getRole(){
        return $this->_role;
    }
    public function setRole($role){
        $this->_role = $role;
    }
    public function getActive(){
        return $this->_active;
    }
    public function setActive($active){
        $this->_active = $active;
    }
    public function getEmail(){
        return $this->_email;
    }
    public function setEmail($email){
        $this->_email = $email;
    }
    
    public function insert($username,$password,$email,$role = 1,$active = 1){
        
        if($username && $password && $email && $role && $active){
            $password = sha1(md5($password.'system'));
           
            if($this->checkExist($username)<0){                 
                $data =  array(
                    'username' =>$username,
                    'password'=>$password,
                    'email'=>$email,
                    'active'=>$active,
                    'role'=>$role);
                $this->db->insert($this->_table_name,$data);
		if($this->checkExist($username)==1)
			return 1;
		}
	}
	return -1;
    }
    public function checkExist($username){
        if($username)
	{			
		
               $select = $this->db->select()
			->from(array("p"=>$this->_table_name))
                        ->where("p.username = ?",$username );
		
		$data = $this->db->fetchRow($select);
                if($data){
                    return 1;
                }
       }	
	return -1;
    }
    public function update(){
        $where  = "id = $this->_id";
	$data =  array(
            'password'=>$this->_password,
            'email'=>$this->_email,
            'role'=>$this->_role,
            'active'=>$this->_active);
	return $this->db->update($this->_table_name,$data,$where);
    }
    public function getById($id){
        if($id)
	{
		$select = $this->db->select()
			->from(array("p"=>$this->_table_name))
                        ->where("p.id = ?",$id );
		
		$data = $this->db->fetchRow($select);
		if($data){
			$this->_id = $data['id'];
			$this->_email = $data['email'];
			$this->_username = $data['username'];
			$this->_password = $data['password'];
                        $this->_role = $data['role'];                       
                        $this->_active = $data['active'];
			
		}
                return $data;
	}
	return -1;
    }
     public function getByUserName($username){
        if($username)
	{
		
		$select = $this->db->select()
			->from(array("p"=>$this->_table_name))
                        ->where("p.username = ?",$username );
		
		$data = $this->db->fetchRow($select);
		if($data){
			$this->_id = $data['id'];
			$this->_email = $data['email'];
			$this->_username = $data['username'];
			$this->_password = $data['password'];
                        $this->_role = $data['role'];                       
                        $this->_active = $data['active'];
			
		}
                return $data;
	}
	return -1;
    }
    public function listAll(){
        $select = $this->db->select()
                ->from($this->_table_name);
	$data = $this->db->fetchAll($select);
	return $data;
    }
    public function delete(){
            $this->getById($this->_id);
            $this->_active = 0;
            return $this->update();
    }
}
?>
